package com.abc.xxx.controller;

import com.abc.xxx.model.User;
import com.abc.xxx.model.View;
import com.abc.xxx.service.ViewService;
import com.abc.xxx.util.Result;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.apache.commons.lang3.StringUtils.isBlank;

@Slf4j
@RestController
@RequestMapping("/views")
public class ViewController {

    private static final String NO_AUTH = "权限不足：只有超级管理员才能";

    @Autowired
    private ViewService viewService;

    @GetMapping("/pages")
    public Result findPages(@AuthenticationPrincipal User user) {
        String oper = "查询页面权限元数据";
        log.info("====>{}", oper);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }
        List<View> list = viewService.findPageMetadata();
        return Result.success(oper).list(list);
    }

    @DeleteMapping("/pages/{pageCode}")
    public Result deletePage(@AuthenticationPrincipal User user,
                                     @PathVariable("pageCode")String pageCode) {
        String oper = "删除页面权限元数据";
        log.info("====>{}", oper);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }
        viewService.deletePageMetadata(pageCode);
        return Result.success(oper);
    }

    @PostMapping("/pages")
    public Result addPage(@AuthenticationPrincipal User user,
                                     @RequestBody String body) {
        String oper = "新增页面权限元数据";
        log.info("====>{}", oper);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }
        View page = JSON.parseObject(body, View.class);
        if (isBlank(page.getName())){
            return Result.error(oper).msg("功能名称不能为空");
        }
        if (isBlank(page.getCode())){
            return Result.error(oper).msg("功能代码不能为空");
        }
        if (viewService.isViewCodeExists(page.getCode())){
            return Result.error(oper).msg("功能代码已存在");
        }
        if (page.getOrder()==null){
            return Result.error(oper).msg("排序号不能为空");
        }
        page.setType(View.PAGE);
        page.setParent(View.ROOT_ELEMENT_PARENT_CODE);
        page.setParents(View.ROOT_ELEMENT_PARENT_CODE+",");
        viewService.save(page);
        return Result.success(oper);
    }

    @PatchMapping("/pages/{pageCode}")
    public Result updatePage(@AuthenticationPrincipal User user,
                             @PathVariable("pageCode") String pageCode,
                             @RequestBody String body) {

        String oper = "更新页面权限元数据";
        log.info("====>{}, pageCode: {}, body: {}", oper, pageCode, body);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }
        View page = JSON.parseObject(body, View.class);
        if (isBlank(page.getName())){
            return Result.error(oper).msg("功能名称不能为空");
        }
        if (isBlank(pageCode)){
            return Result.error(oper).msg("功能代码不能为空");
        }
        if (page.getOrder()==null){
            return Result.error(oper).msg("排序号不能为空");
        }
        page.setCode(pageCode);
        viewService.updateById(page);
        return Result.success(oper);
    }

    @PostMapping("/buttons")
    public Result addButton(@AuthenticationPrincipal User user,
                            @RequestBody String body) {

        String oper = "新增按钮权限元数据";
        log.info("====>{}, body: {}", oper, body);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }

        View button = JSON.parseObject(body, View.class);
        if (isBlank(button.getName())) {
            return Result.error(oper).msg("按钮名称不能为空");
        }
        if (isBlank(button.getCode())) {
            return Result.error(oper).msg("按钮代码不能为空");
        }
        if (button.getOrder()==null) {
            return Result.error(oper).msg("排序号不能为空");
        }
        if (isBlank(button.getParent()) || isBlank(button.getParents())) {
            return Result.error(oper).msg("按钮归属的页面代码不能为空");
        }
        if (viewService.isViewCodeExists(button.getCode())){
            return Result.error(oper).msg("按钮代码已存在");
        }
        button.setType(View.BUTTON);
        viewService.save(button);
        return Result.success(oper);
    }

    @DeleteMapping("/buttons/{buttonCode}")
    public Result deleteButton(@AuthenticationPrincipal User user,
                               @PathVariable("buttonCode") String buttonCode) {

        String oper = "删除按钮权限数据";
        log.info("====>{}, buttonCode: {}", oper, buttonCode);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }
        if (isBlank(buttonCode)) {
            return Result.error(oper).msg("按钮代码不能为空");
        }
        viewService.removeById(buttonCode);
        return Result.success(oper);
    }

    // ---------------

    @GetMapping("/pages/{pageCode}/buttons")
    public Result findButtonsByPageName(@AuthenticationPrincipal User user,
                                        @PathVariable("pageCode") String pageCode) {

        String oper = "查询页面权限下的按钮权限";
        log.info("====>{}, pageCode: {}", oper, pageCode);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }
        List<View> list = viewService.findButtonsByPageCode(pageCode);
        return Result.success(oper).data("list", list);
    }

    @GetMapping("/pages/codes")
    public Result findAllPagesCodes(@AuthenticationPrincipal User user) {

        String oper = "查询所有页面代码";
        log.info("====>{}", oper);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }

        List<String> list = viewService.findAllPagesCodes();
        return Result.success(oper).data("list", list);
    }

    @GetMapping("/buttons")
    public Result findAllButtons(@AuthenticationPrincipal User user) {

        String oper = "查询所有按钮数据";
        log.info("====>{}", oper);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }

        List<View> list = viewService.findAllButtons();
        return Result.success(oper).data("list", list);
    }





    @PatchMapping("/buttons/{code}")
    public Result updateButton(@AuthenticationPrincipal User user,
                               @PathVariable("code") String code,
                               @RequestBody String body) {

        String oper = "更新按钮权限数据";
        log.info("====>{}, code: {}, body", oper, body);
        if (user.isNotRoot()) {
            return Result.error(oper).msg(NO_AUTH + oper);
        }

        View buttonPermission = JSON.parseObject(body, View.class);
        if (isBlank(code)) {
            return Result.error(oper).msg("按钮权限的标识码不能为空");
        }
        if (isBlank(buttonPermission.getName())) {
            return Result.error(oper).msg("按钮权限的名称不能为空");
        }
        View data = new View();
        data.setCode(code);
        data.setName(buttonPermission.getName());
        viewService.updateById(data);
        return Result.success(oper);
    }

}
